package com.pi.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.pi.base.common.BasePage;
import com.pi.base.common.BaseResult;
import com.pi.base.entity.SysParameter;
import com.pi.base.entity.SysParameterExample;
import com.pi.base.mapper.SysParameterMapper;
import com.pi.base.service.BaseService;
import com.pi.base.service.ISysParameterService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("sysParameterService")
public class SysParameterService extends BaseService<SysParameter> implements ISysParameterService{
	
	@Autowired
	private SysParameterMapper sysParameterMapper;

	@Override
	public List<SysParameter> getParamListByCode(String code) throws Exception {
		SysParameterExample example = new SysParameterExample();
		example.createCriteria().andCodeEqualTo(code).andLevelEqualTo(2);
		return sysParameterMapper.selectByExample(example);
	}

	@Override
	public SysParameter getParameterByCode(String code) throws Exception {
		SysParameterExample example =  new SysParameterExample();
		example.createCriteria().andCodeEqualTo(code).andTypeEqualTo(1);
		return getSingelFromList(sysParameterMapper.selectByExample(example));
	}

	@Override
	public SysParameter getParamByCodeAndValue(String code, Object value)
			throws Exception {
		if(StringUtils.isEmpty(code) || value == null){
			return null;
		}
		SysParameterExample example = new SysParameterExample();
		example.createCriteria().andCodeEqualTo(code).andValueEqualTo(value.toString()).andLevelEqualTo(2);
		return getSingelFromList(sysParameterMapper.selectByExample(example));
	}

	@Override
	public BasePage<SysParameter> getParameterListByExample(
			SysParameterExample example,int startPage,int rows) throws Exception {
		 Page<SysParameter> pageInfo = PageHelper.startPage(startPage,rows);
		 List<SysParameter> list = sysParameterMapper.selectByExample(example);
		 return new BasePage<SysParameter>(pageInfo.getTotal(),list);
	}

	@Override
	public SysParameter getParameterById(int parameterId) throws Exception {
		return sysParameterMapper.selectByPrimaryKey(parameterId);
	}

	@Override
	public BaseResult insertSubSysParamter(SysParameter newParameter)
			throws Exception {
		//验证参数名称
		SysParameter searchParameter = new SysParameter();
		searchParameter.setParentId(newParameter.getParentId());
		searchParameter.setName(newParameter.getName());
		if(checkSysParameter(searchParameter)){
			return BaseResult.newErrorResult("参数名称已经存在，请重试");
		}
		searchParameter.setName(null);
		searchParameter.setValue(newParameter.getValue());
		if(checkSysParameter(searchParameter)){
			return BaseResult.newErrorResult("参数值已经存在，请重试");
		}
		sysParameterMapper.insertSelective(newParameter);
		return BaseResult.newSuccessResult("新增成功");
	}
	
	@Override
	public BaseResult updateSubSysParamter(SysParameter newParameter)
			throws Exception {
		//验证参数名称
		SysParameter searchParameter = new SysParameter();
		searchParameter.setParentId(newParameter.getParentId());
		searchParameter.setId(newParameter.getId());
		searchParameter.setName(newParameter.getName());
		if(checkSysParameter(searchParameter)){
			return BaseResult.newErrorResult("参数名称已经存在，请重试");
		}
		searchParameter.setName(null);
		searchParameter.setValue(newParameter.getValue());
		if(checkSysParameter(searchParameter)){
			return BaseResult.newErrorResult("参数值已经存在，请重试");
		}
		newParameter.setParentId(null);// 父ID不需要更改
		sysParameterMapper.updateByPrimaryKeySelective(newParameter);
		return BaseResult.newSuccessResult("更新成功");
	}
	

	private boolean checkSysParameter(SysParameter parameter) throws Exception {
		SysParameterExample example = new SysParameterExample();
		SysParameterExample.Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parameter.getParentId());
		if(parameter.getId() != null){
			criteria.andIdNotEqualTo(parameter.getId());
		}
		if(parameter.getName() != null){
			criteria.andNameEqualTo(parameter.getName());
		}
		if(parameter.getValue() != null){
			criteria.andValueEqualTo(parameter.getValue());
		}
		return sysParameterMapper.selectCountByExample(example) > 0;
	}

	@Override
	public void insertSysParamter(SysParameter newParameter)
			throws Exception {
		sysParameterMapper.insertSelective(newParameter);
	}

	@Override
	public void updateSysParamter(SysParameter newParameter)
			throws Exception {
		sysParameterMapper.updateByPrimaryKeySelective(newParameter);
	}

	@Override
	public void deleteSysParamters(String paramterIds) throws Exception {
		String[] operatorIds = paramterIds.split(",");
		List<Integer> operatorIdList = new ArrayList<Integer>();
		for (String s : operatorIds) {
			operatorIdList.add(Integer.valueOf(s));
		}
		SysParameterExample example = new SysParameterExample();
		example.createCriteria().andIdIn(operatorIdList);
		sysParameterMapper.deleteByExample(example);
	}

}
